package reference;

import java.lang.ref.*;
import java.util.ArrayList;
import java.util.List;

class BigObject {
    byte[] data = new byte[10 * 1024 * 1024];
    private String id;
    public BigObject(String id) {
        this.id = id;
    }

//    @Override
//    protected void finalize() throws Throwable {
//        super.finalize();
//        System.out.println("finalize invoked ");
//    }

    @Override
    public String toString() {
        return id;
    }
}

public class Test1 {

    static ReferenceQueue<BigObject> referenceQueue = new ReferenceQueue<>();

    static void check() {
        Reference<? extends BigObject> ref = referenceQueue.poll();
        if(ref != null)
            System.out.println(ref.get() + " over");
    }

    public static void main(String[] args) throws InterruptedException {
        Reference<BigObject> r = new SoftReference<>(new BigObject("test-1"), referenceQueue);
        List<Reference<BigObject>> list = new ArrayList<>();
        System.gc();
        Thread.sleep(1000);
        check();
    }
}
